package com.jy.adscm.wechat.service.impl;

import com.baomidou.mybatisplus.enums.SqlLike;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jy.adscm.utils.AdscmBaseUtils;
import com.jy.adscm.wechat.bean.*;
import com.jy.adscm.wechat.mapper.mysql.ShCustomerInfoMapper;
import com.jy.adscm.wechat.mapper.mysql.ShMachineMapper;
import com.jy.adscm.wechat.mapper.mysql.WechatUpkeepMapper;
import com.jy.adscm.wechat.mapper.mysql.WechatUserMapper;
import com.jy.adscm.wechat.service.WechatUpkeepService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.wechat.service.WechatUserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zyl
 * @since 2019-10-10
 */
@Service
public class WechatUpkeepServiceImpl extends ServiceImpl<WechatUpkeepMapper, WechatUpkeep> implements WechatUpkeepService {
    @Autowired
    ShCustomerInfoMapper shCustomerInfoMapper;
    @Autowired
    ShMachineMapper shMachineMapper;
    @Autowired
    WechatUserMapper wechatUserMapper;
    @Autowired
    WechatUserRoleService wechatUserRoleService;
    @Override
    public Page<WechatUpkeep> selectWechatUpkeep(Page<WechatUpkeep> page, QueryWechatUpkeep queryWechatUpkeep) {
        Wrapper<WechatUpkeep> wrapper=new EntityWrapper<>();
        WechatUser wechatUser = wechatUserMapper.selectByOpenId(queryWechatUpkeep.getOpenid());
        Boolean isAdmin = wechatUserRoleService.userHasRole(wechatUser.getId().toString(), "5");//是否是管理员

        if (AdscmBaseUtils.getInstance().isNotEmpty(queryWechatUpkeep.getState(),null)){
           wrapper.eq("state",queryWechatUpkeep.getState());
        }
        if (!isAdmin){
            wrapper.eq("repairer",wechatUser.getId());
        }
        page=super.selectPage(page,wrapper);
        List<WechatUpkeep> wechatUpkeeps = page.getRecords();
        for (WechatUpkeep wechatUpkeep : wechatUpkeeps) {
            if (AdscmBaseUtils.getInstance().isNotEmpty(wechatUpkeep.getMachine(),null)){
                ShMachine shMachine = shMachineMapper.selectById(wechatUpkeep.getMachine());
                wechatUpkeep.setMachineName(shMachine);
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(wechatUpkeep.getPid(),null)){
                ShCustomerInfo shCustomerInfo = shCustomerInfoMapper.selectById(wechatUpkeep.getPid());
                wechatUpkeep.setShCustomerInfo(shCustomerInfo);
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(wechatUpkeep.getRepairer(),null)){
                WechatUser wechatUser1 = wechatUserMapper.selectById(wechatUpkeep.getRepairer());
                wechatUpkeep.setRepairerName(wechatUser1.getName());
            }
        }
        page.setTotal(selectCount(wrapper));
        return page;
    }

    @Override
    public boolean updateWechatUpkeep(WechatUpkeep wechatUpkeep) {
        WechatUpkeep oldWechatUpkeep = super.selectById(wechatUpkeep.getId());
        if (wechatUpkeep.getResult()==1){
            if (oldWechatUpkeep.getResult()==0){
                wechatUpkeep.setState(1);
            }
        }
        boolean b = super.updateById(wechatUpkeep);
        return b;
    }

    @Override
    public WechatUpkeep selectWechatUpkeepById(Long id) {
        WechatUpkeep wechatUpkeep = super.selectById(id);
        if (AdscmBaseUtils.getInstance().isNotEmpty(wechatUpkeep.getMachine(),null)){
            ShMachine shMachine = shMachineMapper.selectById(wechatUpkeep.getMachine());
            wechatUpkeep.setMachineName(shMachine);
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(wechatUpkeep.getPid(),null)){
            ShCustomerInfo shCustomerInfo = shCustomerInfoMapper.selectById(wechatUpkeep.getPid());
            wechatUpkeep.setShCustomerInfo(shCustomerInfo);
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(wechatUpkeep.getRepairer(),null)){
            WechatUser wechatUser1 = wechatUserMapper.selectById(wechatUpkeep.getRepairer());
            wechatUpkeep.setRepairerName(wechatUser1.getName());
        }
        return wechatUpkeep;
    }

    @Override
    public boolean insertWechatUpkeep(WechatUpkeep wechatUpkeep, String openid) {
        synchronized (this) {
            WechatUser wechatUser = wechatUserMapper.selectByOpenId(openid);
            wechatUpkeep.setState(0);
            wechatUpkeep.setCreateTime(new Date());
            wechatUpkeep.setRepairer(wechatUser.getId().toString());
            String orderNums="";
            String date = new SimpleDateFormat("yyMMdd").format(new Date());
            Wrapper<WechatUpkeep> wechatUpkeepWrapper=new EntityWrapper<>();
            wechatUpkeepWrapper.like("order_nums",date, SqlLike.RIGHT);
            wechatUpkeepWrapper.orderBy("order_nums",false);
            wechatUpkeepWrapper.last(" limit 1");
            List<WechatUpkeep> wechatUpkeeps = super.selectList(wechatUpkeepWrapper);
            if (wechatUpkeeps!=null && wechatUpkeeps.size()>0){
                orderNums= String.valueOf(Long.valueOf(wechatUpkeeps.get(0).getOrderNums())+1) ;
            }else {
                orderNums=date+"0001";
            }
            wechatUpkeep.setOrderNums(orderNums);

            boolean insert = super.insert(wechatUpkeep);
            return insert;
        }

    }
}
